package com.core.log;

import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.core.framework.tools.Config;
import com.core.framework.tools.PropertiesLoader;
/**
 * 日志管理  
 * @author 刘涛
 * @since Jun 27, 2012
 */
public class Log {
	private static Logger log   ;
	static String logProject="MyDebug";
	static boolean isDev = Config.getProBoolean("dev.debug");
	//日志等级
	static int logLevel = Config.getProInteger("dev.debugLevel", 2);
	static{
		synchronized(Log.class){
			 Properties log4jproperties = PropertiesLoader.getProperties("log4j.properties");
			 PropertyConfigurator.configure(log4jproperties);
			 log = Logger.getLogger(Log.class);
		}
	}
	private Log(){}
	public void setConfigurator(String url){
	
		PropertyConfigurator.configure(url);
	}
	/**
	 * 日志等级为 1级 
	 * @param msg
	 */
	public static void error(Object msg){
		 Exception e = new Exception();
	     StackTraceElement trace = e.getStackTrace()[1];
	     int level =1;
	     if(level<=logLevel)
	     {
	    	 log.error("[Error] " + trace + " - " + msg);
	     }
	}
	/**
	 * 日志等级为 2级 
	 * @param msg
	 */
	public static void debug(Object msg){
		Exception e = new Exception();
        StackTraceElement trace1 = e.getStackTrace()[1];
        int level =2;
        if(isDev && level<=logLevel)
        {
        	log.debug("["+logProject+"] " + trace1 + " - "+msg);
        }
	}
	/**
	 * 日志等级为 3级 
	 * @param msg
	 */
	public static void info(Object msg){
		
		Exception e = new Exception();
        StackTraceElement trace = e.getStackTrace()[1];
        int level =3;
        if( level<=logLevel)
        	log.info("[Info] " + trace + " - " + msg);
	}
	/**
	 * 日志等级为 4级 
	 * @param msg
	 */
	public static void warn(Object msg){
		Exception e = new Exception();
        StackTraceElement trace = e.getStackTrace()[1];
        int level =4;
        if( level<=logLevel)
        	log.warn("[WARN] " + trace + " - " + msg);
		
	}
	
	public static void main(String[] args) {

		Log.debug("dddddd");

	}
	
}
